/*
 * @Author: hongbin
 * @Date: 2022-11-20 22:39:39
 * @LastEditors: hongbin
 * @LastEditTime: 2022-12-23 20:46:54
 * @Description:通用函数
 */

/**
 * 将mongodb的id转换成Date对象
 */
export function mongoIDToDate(_id: string): Date {
    return new Date(parseInt(`${_id.substring(0, 8)}`, 16) * 1000);
}

/**
 * 解决水合阶段服务端的Date和客户端的Date返回格式不同导的水合阶段报错
 * @return y/m/d:h:mi:s
 */
export function mongoIDToDateTime(_id?: string) {
    const date = _id ? new Date(parseInt(`${_id.substring(0, 8)}`, 16) * 1000) : new Date();
    const y = date.getFullYear();
    const m = date.getMonth() + 1;
    const d = date.getDate();
    // console.log(y, m, d);
    const h = date.getHours();
    const mi = date.getMinutes();
    const s = date.getSeconds();
    // console.log(h, mi, s);
    return `${y}/${m}/${d}:${h}:${mi}:${s}`;
}
